:-lib(ic).
/*Q4.1*/
getData(TailleEquipes,NbEquipes,CapaBateaux,NbBateaux,NbConf):-
	TailleEquipes = [](5,5,2,1),
	NbEquipes = 4,
	CapaBateaux = [](7,6,5),
	NbBateaux = 3,
	NbConf = 3.

/*Q4.2*/
defineVars(T,NbEquipes,NbConf,NbBateaux):-
	dim(T,[NbEquipes,NbConf]),
	( for(Indice1,1,NbEquipes),param(T,NbBateaux,NbConf)
	do
		( for(Indice2,1,NbConf),param(T,Indice1,NbBateaux)
		do
			T[Indice1,Indice2]#::1..NbBateaux
		)
	).

/*Résultats

*/

/*Q4.3*/
getVarList(T,L):-
	dim(T,[NbEquipes,NbConf]),
	( for(Indice1,1,NbConf),fromto([],In,Out,L),param(T,NbEquipes)
	do
		( for(Indice2,1,NbEquipes),fromto([],Inc,Outc,Lc),param(T,Indice1)
		do
			Var is T[Indice2,Indice1],
			append(Inc,[Var],Outc)
		),
		append(In,Lc,Out)
	).

/*Résultats
getData(TE, NBE, CB, NBB, NBC), defineVars(T, NBE, NBC, NBB), getVarList(T, L).

TE = [](5, 5, 2, 1)
NBE = 4
CB = [](7, 6, 5)
NBB = 3
NBC = 3
T = []([](_350{1 .. 3}, _419{1 .. 3}, _488{1 .. 3}), [](_559{1 .. 3}, _628{1 .. 3}, _697{1 .. 3}), [](_768{1 .. 3}, _837{1 .. 3}, _906{1 .. 3}), [](_977{1 .. 3}, _1046{1 .. 3}, _1115{1 .. 3}))
L = [_350{1 .. 3}, _559{1 .. 3}, _768{1 .. 3}, _977{1 .. 3}, _419{1 .. 3}, _628{1 .. 3}, _837{1 .. 3}, _1046{1 .. 3}, _488{1 .. 3}, _697{1 .. 3}, _906{1 .. 3}, _1115{1 .. 3}]
Yes (0.00s cpu)
*/

/*Q4.4*/
/*solve(T):-
	getData(_,NBE,_,NBB,NBC),
	defineVars(T,NBE,NBC,NBB),
	pasMemeBateaux(T,NBE,NBC),
	pasMemePartenaire(T,NBE,NBC),
	getVarList(T,L),
	labeling(L).
*/
/*Q4.5*/
pasMemeBateaux(T,NbEquipes,NbConf):-
	dim(T,[NbEquipes,NbConf]),
	( for(Indice1,1,NbEquipes),param(T,NbConf)
	do
		( for(Indice2,1,NbConf), fromto([],In,Out,L), param(T,Indice1)
		do
			V is T[Indice1,Indice2],
			append(In,[V],Out)
		),
		alldifferent(L)
	).
/*
Resultats:

T = []([](1, 2, 3), [](1, 2, 3), [](1, 2, 3), [](1, 2, 3))
Yes (0.00s cpu, solution 1, maybe more) ? ;

T = []([](1, 2, 3), [](1, 2, 3), [](1, 2, 3), [](1, 3, 2))
Yes (0.00s cpu, solution 2, maybe more) ? ;

T = []([](1, 2, 3), [](1, 2, 3), [](1, 3, 2), [](1, 2, 3))
Yes (0.00s cpu, solution 3, maybe more) ? ;

T = []([](1, 2, 3), [](1, 2, 3), [](1, 3, 2), [](1, 3, 2))
Yes (0.00s cpu, solution 4, maybe more) ? ;

T = []([](1, 2, 3), [](1, 3, 2), [](1, 2, 3), [](1, 2, 3))
Yes (0.00s cpu, solution 5, maybe more) ? ;

T = []([](1, 2, 3), [](1, 3, 2), [](1, 2, 3), [](1, 3, 2))
Yes (0.00s cpu, solution 6, maybe more) ? ;

T = []([](1, 2, 3), [](1, 3, 2), [](1, 3, 2), [](1, 2, 3))
Yes (0.00s cpu, solution 7, maybe more) ? ;

On a pas deux fois le même bateau !
*/


/*Q4.6*/
pasMemePartenaire(T,NbEquipes,NbConf):-
	dim(T,[NbEquipes,NbConf]),
	( for(Equipe1,1,NbEquipes),param(T,NbConf,NbEquipes)
	do
		Indice is Equipe1+1,
		( for(Equipe2,Indice,NbEquipes), param(T,Equipe1,NbConf)
		do
						
			( for(Conf,1,NbConf),param(T,Equipe1,Equipe2),fromto(0,In,Out,Tot)
			do
				Bateau1 is T[Equipe1,Conf],
				Bateau2 is T[Equipe2,Conf],
				#=(Bateau1,Bateau2,Ans),
				Out #= In + Ans
			),
			Tot #=< 1
		)
	).
/*
24 solutions
*/

/*Q4.7*/
capaBateaux(T,TailleEquipes,NbEquipes,CapaB,NbBateaux,NbConf):-
	dim(T,[NbEquipes,NbConf]),
	( for(Bateau,1,NbBateaux), param(T,NbEquipes,NbConf,CapaB,TailleEquipes)
	do
		( for(Conf,1,NbConf), param(T,NbEquipes,Bateau,CapaB,TailleEquipes)
		do		
			( for(Equipe,1,NbEquipes),param(T,Bateau,Conf,TailleEquipes),fromto(0,In,Out,TotalPassagers)
			do
				Bateau1 is T[Equipe,Conf],
				#=(Bateau,Bateau1,Cond),
				Inc #= TailleEquipes[Equipe] * Cond,
				Out #= In + Inc
			),
			Capacite is CapaB[Bateau],
			TotalPassagers #=< Capacite
		)
	).

/*
Résultats: 

T = []([](3, 1, 2), [](2, 3, 1), [](1, 2, 3), [](2, 1, 3))
Yes (0.01s cpu, solution 11, maybe more) ? ;

T = []([](3, 2, 1), [](2, 1, 3), [](1, 3, 2), [](2, 3, 1))
Yes (0.01s cpu, solution 12)
*/

solve(T):-
	getData(TE,NBE,CB,NBB,NBC),
	defineVars(T,NBE,NBC,NBB),
	pasMemeBateaux(T,NBE,NBC),
	pasMemePartenaire(T,NBE,NBC),
	capaBateaux(T,TE,NBE,CB,NBB,NBC),
	getVarList(T,L),
	labeling(L).	


/************************ 2e PARTIE ********************************/
getDataPart2(TailleEquipes,NbEquipes,CapaBateaux,NbBateaux,NbConf):-
	TailleEquipes = [](7,6,5,5,5,4,4,4,4,4,4,4,4,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2),
	NbEquipes = 29,
	CapaBateaux = [](10,10,9,8,8,8,8,8,8,7,6,4,4),
	NbBateaux = 13,
	NbConf = 7.

/*trier la liste*/
getVarList2(T,L):-
	dim(T,[NbEquipes,NbConf]),
	( for(Indice1,1,NbConf),fromto([],In,Out,L),param(T,NbEquipes)
	do	
		( for(Indice2,1,NbEquipes),fromto([],Inc,Outc,Lc),param(T,Indice1)
		do
			Var is T[Indice2,Indice1],
			append(Inc,[Var],Outc)
		),
		tri(Lc,Lctriee),
		append(In,Lctriee,Out)
	).

tri([],[]).
tri([A],[A]).
tri([A|R],[A,B|R1]):-
	append(M,[B],R),
	tri(M,R1).
		

solvePart2(T):-
	getDataPart2(TE,NBE,CB,NBB,NBC),
	defineVars(T,NBE,NBC,NBB),
	pasMemeBateaux(T,NBE,NBC),
	pasMemePartenaire(T,NBE,NBC),
	capaBateaux(T,TE,NBE,CB,NBB,NBC),
	getVarList2(T,L),
	labeling(L).

/*
Résultats

[eclipse 3]: solvePart2(T).
lists.eco  loaded in 0.00 seconds

T = []([](1, 2, 3, 4, 5, 6, 7), [](2, 1, 4, 3, 6, 5, 8), [](3, 4, 1, 2, 8, 7, 9), [](4, 3, 5, 1, 2, 9, 10), [](5, 6, 2, 7, 1, 3, 4), [](6, 5, 7, 2, 1, 4, 3), [](6, 7, 8, 5, 2, 1, 11), [](7, 5, 6, 8, 9, 1, 2), [](8, 9, 7, 10, 4, 11, 5), [](8, 10, 9, 6, 11, 3, 2), [](9, 8, 12, 13, 7, 2, 6), [](10, 9, 8, 11, 13, 12, 1), [](12, 13, 11, 9, 10, 8, 1), [](11, 10, 13, 3, 8, 9, 4), [](11, 12, 10, 7, 3, 13, 9), [](13, 11, 10, 9, 6, 1, 5), [](13, 8, 11, 12, 4, 10, 3), [](10, 11, 9, 8, 12, 2, 3), [](9, 11, 6, 12, 10, 5, 13), [](9, 7, 10, 11, 12, 8, 2), [](7, 8, 9, 10, 3, 4, 1), [](7, 6, 5, 9, 11, 2, 8), [](5, 7, 1, 8, 3, 10, 13), [](4, 1, 6, 5, 3, 2, 12), [](3, 2, 4, 1, 7, 11, 12), [](3, 1, 2, 6, 9, 10, 11), [](2, 4, 3, 1, 9, 8, 6), [](2, 3, 1, 6, 7, 4, 5), [](1, 3, 2, 5, 4, 7, 6))
Yes (6.65s cpu, solution 1, maybe more) ? ;

*/